package com.iyao_app.preview;

import android.graphics.RectF;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.DraweeView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/27.
 */

public class DefaultOnDoubleTapListener implements GestureDetector.OnDoubleTapListener {
    protected Attacher mAttacher;

    public DefaultOnDoubleTapListener(Attacher attacher) {
        this.setPhotoDraweeViewAttacher(attacher);
    }

    public boolean onSingleTapConfirmed(MotionEvent e) {
        if(this.mAttacher == null) {
            return false;
        } else {
            DraweeView<GenericDraweeHierarchy> draweeView = this.mAttacher.getDraweeView();
            if(draweeView == null) {
                return false;
            } else {
                if(this.mAttacher.getOnPhotoTapListener() != null) {
                    RectF displayRect = this.mAttacher.getDisplayRect();
                    if(null != displayRect) {
                        float x = e.getX();
                        float y = e.getY();
                        if(displayRect.contains(x, y)) {
                            float xResult = (x - displayRect.left) / displayRect.width();
                            float yResult = (y - displayRect.top) / displayRect.height();
                            this.mAttacher.getOnPhotoTapListener().onPhotoTap(draweeView, xResult, yResult);
                            return true;
                        }
                    }
                }

                if(this.mAttacher.getOnViewTapListener() != null) {
                    this.mAttacher.getOnViewTapListener().onViewTap(draweeView, e.getX(), e.getY());
                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    public boolean onDoubleTap(MotionEvent event) {
        if(this.mAttacher == null) {
            return false;
        } else {
            try {
                float scale = this.mAttacher.getScale();
                float x = event.getX();
                float y = event.getY();
                if(scale < this.mAttacher.getMediumScale()) {
                    this.mAttacher.setScale(this.mAttacher.getMediumScale(), x, y, true);
                } else if(scale >= this.mAttacher.getMediumScale() && scale < this.mAttacher.getMaximumScale()) {
                    this.mAttacher.setScale(this.mAttacher.getMaximumScale(), x, y, true);
                } else {
                    this.mAttacher.setScale(this.mAttacher.getMinimumScale(), x, y, true);
                }
            } catch (Exception var5) {
                ;
            }

            return true;
        }
    }

    public boolean onDoubleTapEvent(MotionEvent event) {
        return false;
    }

    public void setPhotoDraweeViewAttacher(Attacher attacher) {
        this.mAttacher = attacher;
    }
}

